﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  JoinNowInstantMessageActivity.cs
//    
// Abstract:
//
//  Activity for sending the other party in an instant message call
//  to a conference call or live meeting.
//  
//--

using System;
using System.Diagnostics;
using System.Workflow.ComponentModel;
using Microsoft.Rtc.Workflow.Activities;
using Microsoft.Rtc.Collaboration;

namespace Microsoft.EnhancedReminders.Dispatcher.Workflow
{
    /// <summary>
    /// Workflow activity for joining a conference via instant message.
    /// </summary>
    public partial class JoinNowInstantMessageActivity : JoinNowActivityBase
    {
        #region Fields

        private static readonly DependencyProperty CallProperty =
            DependencyProperty.Register("Call", typeof(Call), typeof(JoinNowInstantMessageActivity));

        private static readonly InstanceDependencyProperty NotificationProperty =
            InstanceDependencyProperty.Register("Notification", typeof(CalendarNotificationItem), typeof(JoinNowInstantMessageActivity));

        private Guid _subscriptionId;

        #endregion Fields

        /// <summary>
        /// ctor.
        /// </summary>
        public JoinNowInstantMessageActivity()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The call to be transferred.
        /// </summary>
        public override Call Call
        {
            get
            {
                return (Call)this.GetValue(CallProperty);
            }
            set
            {
                this.SetValue(CallProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the address of the conference to join
        /// </summary>
        public override CalendarNotificationItem Notification
        {
            get
            {
                return InstanceDependencyHelper.GetValue<JoinNowInstantMessageActivity>(this, NotificationProperty) as CalendarNotificationItem;
            }
            set
            {
                InstanceDependencyHelper.SetValue<JoinNowInstantMessageActivity>(this, NotificationProperty, value);
            }
        }

        /// <summary>
        /// Subscribes the activity to the JoinNowVoiceService
        /// </summary>
        /// <param name="parentContext">The parent context.</param>
        protected override void SubscribeToJoinNowService(ActivityExecutionContext parentContext)
        {
            JoinNowInstantMessageService joinNowService = parentContext.GetService<JoinNowInstantMessageService>();
            _subscriptionId = joinNowService.RegisterJoinNowOperation(QueueName, Call, Notification);
        }

        /// <summary>
        /// Unsubscribes the activity from the JoinNowVoiceService.
        /// </summary>
        /// <param name="parentContext">The parent context.</param>
        protected override void UnsubscribeFromJoinNowService(ActivityExecutionContext parentContext)
        {
            JoinNowInstantMessageService joinNowService = parentContext.GetService<JoinNowInstantMessageService>();
            Debug.Assert(joinNowService != null, "joinNowService != null");

            joinNowService.UnregisterJoinNowOperation(_subscriptionId);
            _subscriptionId = Guid.Empty;
        }
    }
}
